package models

import (
	"fitness-srv/basic/config"
	"gorm.io/gorm"
	"time"
)

// 课程表
type Course struct {
	gorm.Model
	UserId   int        `json:"user_id" gorm:"type:int;comment:'用户id'"`
	Title    string     `json:"title" gorm:"type:varchar(100);comment:'标题'"`
	Desc     string     `json:"desc" gorm:"type:varchar(255);comment:'描述'"`
	Price    string     `json:"price" gorm:"type:varchar(30);comment:'价格'"`
	Cover    string     `json:"cover" gorm:"type:varchar(300);comment:'封面'"`
	Stock    int        `json:"stock" gorm:"type:int;comment:'库存'"`
	StarTime *time.Time `json:"star_time" gorm:"type:datetime;comment:'开始时间'"`
	EndTime  *time.Time `json:"end_time" gorm:"type:datetime;comment:'结束时间'"`
	Status   int        `json:"status" gorm:"type:int;comment:'状态 1-上架 2-下架"`
}

func (c *Course) TableName() string {
	return "course"
}

func (c *Course) AddCourse() error {
	return config.DB.Create(&c).Error
}

func (c *Course) GetCourseInfo(id int64) error {
	return config.DB.Model(&Course{}).Where("id = ?", id).Limit(1).Find(&c).Error
}

func (c *Course) GetCourseList() (courseList []*Course, err error) {
	config.DB.Model(&Course{}).Find(&courseList)
	return courseList, err
}

func (c *Course) UpdateCourse(id int64) error {
	return config.DB.Model(&Course{}).Where("id = ?", id).Updates(&c).Error
}
